import jsonpath


class AssertUtil:

    def jsonpath_value(self, res, expression):
        """

        :param res: res.json()
        :param expression: $.token
        :return:
        """
        result = jsonpath.jsonpath(res, expression)
        if not result:
            return
        elif len(result) == 1:
            return result[0]
        else:
            return result

    def assert_servel(self, res, vilidate):
        """

        :param res: res.json()
        :param vilidate: vilidate
        :return:
        """
        for vilidate_value in vilidate:
            for key, value in vilidate_value.items():
                # 获取实际结果
                actual_results = self.jsonpath_value(res, value[0])
                # 预期结果
                expected_results = value[1]
                if key in ["contains", "con"]:
                    self.contains(actual_results, expected_results)

    def contains(self, actual_results, expected_results):
        assert expected_results in actual_results, f"{expected_results} in {actual_results}"
